package buffer.intristing;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class classD {

    public static Map<String, TimeAndString> beginMap = new HashMap();
    public static Map<String, TimeAndString> endMap = new HashMap();

    public static List<TimeAndString> result = new ArrayList<TimeAndString>();

    static long maxTime = 2000;

    public static void main(String[] args) throws IOException {
        int count = 0;
        FileInputStream in = null;
        FileOutputStream out = null;
        in = new FileInputStream("C:\\Users\\Dzq\\Desktop\\out.log-20181101_10.33.100.96.txt");
        out = new FileOutputStream("C:\\Users\\Dzq\\Desktop\\outPut2.txt");

        List<Integer> b = new ArrayList<Integer>();
        int num = 0;
        try {
            while ((count = in.read()) != -1) {
                //System.out.print((char)count);
//                    System.out.println(count);
                if (count == 10) {
                    b.add(count);
                    setMap(b);
                    num = 0;
                    b = new ArrayList<Integer>();
                } else {
                    b.add(count);
                    num++;
                }
            }
            outPut(out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            in.close();
            out.close();
        }


    }


    public static void setMap(List<Integer> list) throws ParseException {
        int timeStartSub = 1;
        int timeEndSub = 20;
        int threadStartSub = 27;
        int threadEndSub = 50;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            sb.append((char) list.get(i).intValue());
        }
        String sbs = sb.toString();
        if (!sbs.contains("checkPayerLimit,params")&&!sbs.contains("checkPayerLimit result is")) {
            return ;
        }
        String time = sbs.substring(timeStartSub, timeEndSub);
        SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        long nowTime = sf.parse(time).getTime();

        String thread = sb.substring(threadStartSub, threadEndSub);
        while (true) {
            if (thread.contains("]")) {
                threadEndSub -= 1;
                thread = sb.substring(threadStartSub, threadEndSub);
            } else {
                break;
            }
        }
        if (sbs.contains("checkPayerLimit,params")) {
            beginMap.put(thread, new TimeAndString(sbs, nowTime));
        } else if (sbs.contains("checkPayerLimit result is")) {
            endMap.put(thread, new TimeAndString(sbs, nowTime));
            if (beginMap.get(thread) != null && endMap.get(thread) != null) {
                TimeAndString begin = beginMap.get(thread);
                TimeAndString end = endMap.get(thread);
                long timeing = end.getTime() - begin.getTime();
                if (timeing > maxTime) {
                    result.add(begin);
                    begin.setEndBody(end.getBody());
                    begin.setTime(timeing);
                }
            }
        }
    }


    public static void outPut(FileOutputStream outputStream) throws IOException {
        for (int i=0;i<result.size();i++) {
            TimeAndString now=result.get(i);
            outputStream.write(now.getBody().getBytes());
            outputStream.write(now.getEndBody().getBytes());
        }
    }
}
